package com.sotb.sender;

import java.io.InputStream;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 
 * @author  lgb
 * 联系人列表,以便添加到好友列表中
 */
public class ContactListActivity extends ListActivity{
	private ListView contractList = null;
	private MyListAdapter mAdapter = null;
	private Context mContext = null;
	/*数据库Phone表字段*/
	private static final String [] PHONES_PROJECTION = {Phone.DISPLAY_NAME, Phone.NUMBER, Phone.PHOTO_ID, Phone.CONTACT_ID};
	/*联系人显示名称*/
	private static final int PHONES_DISPLAY_NAME_INDEX = 0;
	/*电话号码*/
	private static final int PHONES_NUMBER_INDEX = 1;
	/*头像ID*/
	private static final int PHONES_PHOTO_ID_INDEX = 2;
	/*联系人ID*/
	private static final int PHONES_CONTACT_ID_INDEX = 3;
	/*联系人名称线性表*/
	private ArrayList<String> contactsName = new ArrayList<String>();
	/*联系人号码线性表*/
	private ArrayList<String> contactsNumber = new ArrayList<String>();
	/*联系人头像线性表*/
	private ArrayList<Bitmap> contactsPhoto = new ArrayList<Bitmap>();
	/*标题*/
	private final String TITLE = "请选择一位好友，点击确定";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitle(TITLE);
		/*获得context对象*/
		mContext = this;
		contractList = this.getListView();
		/*获取联系人信息*/
		getContactList();
		mAdapter = new MyListAdapter(this);
		setListAdapter(mAdapter);
		contractList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				final String name = contactsName.get(position);
				final String number = contactsNumber.get(position);
//				Bitmap photo = contactsPhoto.get(position);
				Builder dialog = new AlertDialog.Builder(mContext);
				dialog.setMessage("将号码为" + number+ "的联系人 "+ name +"设置为好友?");
				dialog.setPositiveButton(Constant.YES, new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Intent intent = new Intent();
						intent.setClass(mContext, FriendsActivity.class);
						Bundle bundle = new Bundle();
						bundle.putString(Constant.NAME, name);
						bundle.putString(Constant.NUMBER, number);
						intent.putExtras(bundle);
						startActivity(intent);
						finish();
					}
				});
				dialog.setNegativeButton(Constant.NO, new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				});
				dialog.show();
			}
		});
	}
	
	private void getContactList() {
		// TODO Auto-generated method stub
		ContentResolver resolver = mContext.getContentResolver();
		Cursor cursor = resolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null);
		if (cursor != null){
			while(cursor.moveToNext()){
				//得到手机号码
				String contactNumber = cursor.getString(PHONES_NUMBER_INDEX);
				//如果号码字段为空,跳过当前循环
				if (TextUtils.isEmpty(contactNumber))
					continue;
				//得到联系人名称
				String contactName = cursor.getString(PHONES_DISPLAY_NAME_INDEX);
				//得到联系人ID
				long contactID = cursor.getLong(PHONES_CONTACT_ID_INDEX);
				//得到联系人头像ID
				long photoID = cursor.getLong(PHONES_PHOTO_ID_INDEX);
				//联系人头像
				Bitmap contactPhoto = null;
				//phtotID>0表示联系人有头像,没有的话给他一个默认头像
				if (photoID > 0){
					Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactID);
					InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
					contactPhoto = BitmapFactory.decodeStream(input);
				}else{
					contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
				}
				//将获得的信息添加到线性表中
				contactsName.add(contactName);
				contactsNumber.add(contactNumber);
				contactsPhoto.add(contactPhoto);
			}
			cursor.close();
		}
	}

	class MyListAdapter extends BaseAdapter {  
	    public MyListAdapter(Context context) {  
	        mContext = context;  
	    }  
	  
	    public int getCount() {  
	        //设置绘制数量  
	        return contactsName.size();  
	    }  
	  
	    @Override  
	    public boolean areAllItemsEnabled() {  
	        return false;  
	    }  
	  
	    public Object getItem(int position) {  
	        return position;  
	    }  
	  
	    public long getItemId(int position) {  
	        return position;  
	    }

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView iamge = null;
			TextView name = null;
			TextView number = null;
			if (convertView == null || position < contactsNumber.size()) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.contact_list_item, null);
				iamge = (ImageView) convertView.findViewById(R.id.contact_photo);
				name = (TextView) convertView.findViewById(R.id.contact_name);
				number = (TextView) convertView.findViewById(R.id.contact_number);
			}
			// 绘制联系人名称
			name.setText(contactsName.get(position));
			// 绘制联系人号码
			number.setText(contactsNumber.get(position));
			// 绘制联系人头像
			iamge.setImageBitmap(contactsPhoto.get(position));
			return convertView; 
		} 
	}
	
}